<template>
  <div class="Login">
    <Head msg="用户登录"></Head>
    <!--        <div class="login-title">用户登录</div>-->
    <div class="login-list">
      <div>手机号：</div>
      <input type="number" placeholder="请输入手机号" v-model="tel" />
    </div>
    <div class="login-list">
      <div>密码：</div>
      <input type="password" placeholder="请输入密码" v-model="psd" />
    </div>
    <div class="login-list">
      <div>验证码：</div>
      <input
        type="text"
        placeholder="请输入验证码"
        class="login-code-input"
        v-model="code"
      />
    </div>
    <div class="login-code-main">
      <img class="login-code" :src="codeurl" />
      <div class="code-fresh-btn" @click="codeFresh">刷新</div>
    </div>
    <div class="login-btn" @click="login">立即登录</div>
    <div class="foget-psd" @click="toreport">忘记密码</div>

    <div v-if="codeshow" class="screen"></div>
    <div v-if="codeshow" class="code-main">
      <img
        class="icon-pic"
        src="../../public/img/icons/icon-clear.png"
        @click="closereport"
      />
      <img class="service-code" :src="this.backcode" />
      <div class="code-title">请关注公众号后找回密码</div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component } from 'vue-property-decorator';
import Sence from '../sence/Sence';

import Head from '@/components/Head.vue';

@Component({
  components: {
    Head,
  },
})
export default class Login extends Sence {
  public tel: string = '';
  public psd: string = '';
  public code: string = '';
  public backcode: string = '';
  public codeshow: boolean = false;
  public codeurl: string = this.apiurl + 'captcha.html';

  public mounted(): void {
    this.post('api/v1/getSiteConfig', {}).then((res: any) => {
      this.backcode = res.data.qrcode;
    });
  }

  public codeFresh(): void {
    const that = this;
    that.codeurl = this.apiurl + 'captcha.html?d=' + Math.random();
  }

  public login(): void {
    if (this.tel.length != 11) {
      this.toast('手机号输入错误！', 'warn');
      return;
    } else if (this.psd == '') {
      this.toast('密码不能为空！', 'warn');
      return;
    } else if (this.code == '') {
      this.toast('验证码不能为空！', 'warn');
      return;
    }

    const that = this;
    that
      .post('api/v1/doLogin', {
        mobile: that.tel,
        passwd: that.psd,
        captcha: that.code,
      })
      .then((res: any) => {
        that.toast('登录成功');
        that.set('user', res.data.userInfo);
        setTimeout(() => {
          (<any>that).$router.push('/mine');
        }, 2000);
      });
  }

  //举报
  public toreport(): void {
    this.codeshow = true;
  }

  //关闭举报
  public closereport(): void {
    this.codeshow = false;
  }
}
</script>

<style scoped lang="stylus">
input {
  border: 1px solid #808080;
  height: 25px;
}

.login-title {
  height: 80px;
  text-align: center;
  background: #4876ff;
  color: white;
  font-weight: bold;
  font-size: 16px;
  line-height: 80px;
  margin-bottom: 50px;
}

.foget-psd {
  margin: auto;
  width: 30vw;
  margin-top: 8px;
  font-size: 14px;
  text-align: right;
  color: blue;
  text-decoration: underline;
}

.login-list {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-top: 20px;

  > div {
    width: 33vw;
    text-align: right;
  }
}

.login-code-main {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin-top: 20px;

  .login-code {
    width: 140px;
    height: 40px;
  }

  .login-code-input {
    width: 30vw;
  }

  .code-fresh-btn {
    padding: 0 10px;
    height: 30px;
    line-height: 30px;
    background: #ff6347;
    color: white;
    font-weight: bold;
    margin-left: 10px;
    font-size: 12px;
  }
}

.login-btn {
  margin: auto;
  margin-top: 12vh;
  background: red;
  color: white;
  width: 30vw;
  font-weight: bold;
  padding: 8px 0;
  font-size: 15px;
}

.code-main {
  position: fixed;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 99;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 60vw;
  height: 60vw;
  background: white;
}

.icon-pic {
  position: absolute;
  width: 8vw;
  height: 8vw;
  right: 1vw;
  top: 1vw;
}

.screen {
  position: fixed;
  height: 100vh;
  width: 100vw;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 98;
  background: rgba(0, 0, 0, 0.3);
}

.service-code {
  width: 50vw;
  height: 50vw;
}

.code-title {
  font-size: 14px;
}
</style>
